package com.woniu.micropropertyserver.mapper;


import com.woniu.microcommonsserver.entity.RelativesInfoEntity;
import com.woniu.microcommonsserver.entity.RenterInfoEntity;
import com.woniu.micropropertyserver.util.PageRest;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface RenterInfoMapper {

    @ResultType(RenterInfoEntity.class)
    @Select("select id,name,sex,card_type cardType,card_id cardId,phone,item,house_num houseNum,headimg, fk_owner_id fkOwnerId,status from renter_info where status=0")
    List<RenterInfoEntity> findall();

   List<RenterInfoEntity> findAllByParams(@Param("p") PageRest pageRest, @Param("r") RenterInfoEntity renterInfo);

    @ResultType(RenterInfoEntity.class)
     @Results({
          @Result(id = true,property = "id",column = "id"),
          @Result(property = "ownerInfo",column ="fk_owner_id",one = @One(select = "com.woniu.micropropertyserver.mapper.OwnerMapper.findNameById",fetchType = FetchType.LAZY))
  })
    @Select("select id,name,sex,card_type cardType,card_id cardId,phone,item,house_num houseNum,headimg, fk_owner_id fkOwnerId from renter_info where id=#{id}")
    RenterInfoEntity findById(Long id);

    @ResultType(RenterInfoEntity.class)
    @Select("select id,name,sex,card_type cardType,card_id cardId,phone,item,house_num houseNum,headimg, fk_owner_id fkOwnerId from renter_info where fk_owner_id=#{id}  and status=0 ")
    List<RenterInfoEntity> findByOwner(Long id);

    @Update("<script>" +
            " update renter_info" +
            "<set>" +
            "<if test='r.name!=null'>" +
            "name=#{r.name}," +
            "</if>" +
            "<if test='r.sex!=null'>" +
            "sex=#{r.sex}," +
            "</if>" +
            "<if test='r.cardType!=null'>" +
            "card_type=#{r.cardType}," +
            "</if>" +
            "<if test='r.cardId!=null'>" +
            "card_id=#{r.cardId}," +
            "</if>" +
            "<if test='r.houseNum!=null'>" +
            "house_num=#{r.houseNum}," +
            "</if>" +
            "<if test='r.item!=null'>" +
            "item=#{r.item}," +
            "</if>" +
            "<if test='r.headimg!=null'>" +
            "headimg=#{r.headimg}," +
            "</if>" +
            "<if test='r.fkOwnerId!=null'>" +
            "fk_owner_id=#{r.fkOwnerId}," +
            "</if>" +
             "</set>where id=#{r.id}" +
            "</script>")

    int updateInfo(@Param("r") RenterInfoEntity renterInfo);

    @Update("update  renter_info set status=1 where id=#{id}")
    int delInfo(Long id);

    @Options(
            useGeneratedKeys = true,
            keyProperty = "r.id"
    )
     @Insert("insert into renter_info(id,name,sex,card_type,card_id,phone,item,house_num,headimg, fk_owner_id,status )values(null,#{r.name},#{r.sex},#{r.cardType},#{r.cardId},#{r.phone},#{r.item},#{r.houseNum},#{r.headimg},#{r.fkOwnerId},0 ) ")
    Long saveInfo(@Param("r") RenterInfoEntity renterInfo);

    @ResultType(RenterInfoEntity.class)
    @Select("select id,name,card_id cardId from renter_info where card_id=#{id} ")
    RenterInfoEntity findByCard(String id);

}
